﻿--
--
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'spZeusPickingAdHoc' AND type = 'P')
BEGIN
  DROP PROCEDURE spZeusPickingAdHoc
END
GO

-- HU - Rakomány kiszedés
-- EN - Picking Ad Hoc
--
CREATE PROCEDURE spZeusPickingAdHoc
(@AccessToken INTEGER, @PickingID INTEGER, @Code NVARCHAR(20), @Quantity DECIMAL(18,6))
 AS
BEGIN
  DECLARE
    @Err INTEGER,
    @Rows INTEGER,
    @UserID INTEGER,
    @Lang NVARCHAR(10)

  DECLARE
    @ID INTEGER

  EXEC @Err = spAccessTokenCheck @AccessToken, @UserID OUTPUT, @Lang OUTPUT

  IF (@Err != 0)
    GOTO __ERROR__


  IF (ISNULL(@Quantity, 0) <= 0) BEGIN

    IF (@Lang = 'HU')
      RAISERROR (N'Mennyiség nem lehet 0 vagy negatív !', 16, 1)

    IF (@Lang = 'EN')
      RAISERROR (N'Quantity could not benn less than Zero !', 16, 1)

    IF (@Lang = 'DE')
      RAISERROR (N'Menge nicht null oder negativ sein kann !', 16, 1)

    GOTO __ERROR__
  END

  DECLARE
    @Status VARCHAR(1)

  SELECT @Status = Status
    FROM ZeusPicking
   WHERE ID = @PickingID

  SELECT @Rows = @@ROWCOUNT

  IF (@Rows != 1) OR (@Status != 'O') BEGIN

    IF (@Lang = 'HU')
      RAISERROR (N'Nincs ilyen Kiszedés ID %d !', 16, 1, @PickingID)

    IF (@Lang = 'EN')
      RAISERROR (N'There is no such picking ID %d !', 16, 1, @PickingID)

    IF (@Lang = 'DE')
      RAISERROR (N'gibt es keine solche Kommissionierung ID %d !', 16, 1, @PickingID)

    GOTO __ERROR__
  END

  --
  --
  BEGIN TRY

    INSERT INTO ZeusPickingItem
      (PickingID, Code, ArticleCode, ArticleName, Expired, LocationCode, Quantity, UnitCode, PickedQua)
    SELECT @PickingID, Code, ArticleCode, ArticleName, Expired, LocationCode, @Quantity, UnitCode, @Quantity
      FROM ZeusPackage
     WHERE Code = @Code;

  END TRY
  BEGIN CATCH

    IF (@Lang = 'HU')
      RAISERROR (N'Kiszedési tétel mentése nem sikerült !', 16, 1)

    IF (@Lang = 'EN')
      RAISERROR (N'Failed to save picking item !', 16, 1)

    IF (@Lang = 'DE')
      RAISERROR (N'Sparen Kommissionierung Artikel fehlgeschlagen !', 16, 1)

    GOTO __ERROR__

  END CATCH


  -- HU - Visszaadja ennek a Rakománynak az adatait
  -- EN - Get the package details
  --
  SELECT Code, ArticleCode, ArticleName, CAST(Expired as DATE) Expired, LocationCode, @Quantity Quantity, UnitCode, N'' Notes
    FROM ZeusPackage
   WHERE Code = @Code
    
-- Well done.
--
  RETURN(0)

-- We have a problem !
--
__ERROR__:
  RETURN(1)
END
GO
